package com.bjpowernode.rbac.service.impl;

import com.bjpowernode.rbac.entity.Employee;
import com.bjpowernode.rbac.entity.Permission;
import com.bjpowernode.rbac.exception.LoginException;
import com.bjpowernode.rbac.mapper.EmployeeMapper;
import com.bjpowernode.rbac.mapper.PermissionMapper;
import com.bjpowernode.rbac.query.EmployeeQuery;
import com.bjpowernode.rbac.query.QueryObject;
import com.bjpowernode.rbac.service.EmployeeService;
import com.bjpowernode.rbac.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jy
 * @version 1.0
 * @date 2022/2/8 18:41
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public PageInfo<Employee> selectEmployeeByCondition(EmployeeQuery employeeQuery) {
        PageHelper.startPage(employeeQuery.getCurrentPage(),employeeQuery.getPageSize());
        List<Employee> list = employeeMapper.selectByCondition(employeeQuery);
        return new PageInfo<>(list);
    }

    @Override
    public List<Employee> selectAllSellers() {
        return employeeMapper.selectAllSellers();
    }

    /**
     * 查找移交里面的新营销人员
     * @return
     */
    @Override
    public List<Employee> selectAllNewSellers() {
        return employeeMapper.selectAllNewSellers();
    }

    @Override
    public Employee selectEmpById(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    /**
     * 若要修改员工已拥有的权限，则需要把之前的关系解绑删除
     * @param employee
     * @param roleIds
     */
    @Override
    public void saveOrUpdate(Employee employee, Long[] roleIds) {
        /**
         * 编辑
         */
        if (employee.getId() != null){
            employeeMapper.updateByPrimaryKeySelective(employee);
            //解绑删除原本的员工角色关系
            employeeMapper.deleteRelation(employee.getId());
        }else {
            //添加
            employeeMapper.insert(employee);
        }

        //重新绑定角色和员工的关系，因为修改了
        if (roleIds != null && roleIds.length > 0){
            for (Long roleId : roleIds) {
                employeeMapper.insertRelation(employee.getId(),roleId);
            }
        }

    }

    @Override
    public void delete(Long id) {
        if (id != null) {
            // 解除员工和角色的关系
            employeeMapper.deleteRelation(id);
            employeeMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public void login(String userName, String password) {
        Employee employee = employeeMapper.selectEmpByUsernameAndPwd(userName,password);
        //如果登录失败
        if (employee == null){
            throw new LoginException("账号或密码输入错误！");
        }else {
            // 如果登录成功，把用户信息保存到session中
            UserContext.saveEmployee(employee);
            //把用户的权限信息存入session中
            List<Permission> permissions = permissionMapper.selectPermissionByEmpId(employee);
            List<String> permissionsStr = permissions.stream().map(Permission::getExpression).collect(Collectors.toList());
            UserContext.savePermission(permissionsStr);
        }
    }

    @Override
    public void logout() {
        UserContext.saveEmployee(null);
    }


}
